using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif


public class RedDotView : MonoBehaviour, IRedDotListener
{
    public bool showCount;
    public string treePath;

    public GameObject red;
    public GameObject redWithCount;
    public Text redCount;
    public GameObject news;

    private void Awake()
    {
        SetNone();
    }

    private void OnEnable()
    {
        if (!string.IsNullOrEmpty(treePath))
        {
            RedDot.Register(this);
            UpdateView();
        }
    }
    
    private void OnDisable()
    {
        RedDot.UnRegister(this);
    }
    
    private void OnDestroy()
    {
        RedDot.UnRegister(this);
    }
    
    /// <summary>
    /// 重新绑定路径
    /// </summary>
    /// <param name="path">监听的红点路径</param>
    /// <param name="bShowCount">是否显示红点数量</param>
    public void ReBind(string path, bool bShowCount)
    {
        if (string.IsNullOrEmpty(path))
        {
            RedDot.UnRegister(this);
            SetNone();
            return;
        }
        this.treePath = path;
        this.showCount = bShowCount;
        RedDot.Register(this);
        if (gameObject.activeInHierarchy)
        {
            UpdateView();
        }
    }

    /// <summary>
    /// 显示红点
    /// </summary>
    public void SetRed()
    {
        SetGoActive(red, true);
        SetGoActive(redWithCount, false);
        SetGoActive(news, false);
    }

    public void SetGoActive(GameObject go, bool isActive)
    {
        go.SetActive(isActive);
    }
    
    /// <summary>
    /// 显示红点和数量
    /// </summary>
    /// <param name="count"></param>
    public void SetRedCount(int count)
    {
        SetGoActive(red, false);
        SetGoActive(redWithCount, true);
        redCount.text = count.ToString();
        SetGoActive(news, false);
    }
    
    /// <summary>
    /// 显示New
    /// </summary>
    public void SetNew()
    {
        SetGoActive(red, false);
        SetGoActive(redWithCount, false);
        SetGoActive(news, true);
    }
    
    /// <summary>
    /// 不显示
    /// </summary>
    public void SetNone()
    {
        SetGoActive(red, false);
        SetGoActive(redWithCount, false);
        SetGoActive(news, false);
    }

    public void UpdateView()
    {
        var count = RedDot.Get(treePath, RedDot.RedDotType.news);
        if (count > 0)
        {
            SetNew();
        }
        else
        {
            count = RedDot.Get(treePath, RedDot.RedDotType.red);
            if (count > 0)
            {
                if (showCount)
                {
                    SetRedCount(count > 99 ? 99 : count);
                }
                else
                {
                    SetRed();
                }
            }
            else
            {
                SetNone();
            }
        }
    }

    public string GetListenPath()
    {
        return treePath;
    }

    int IRedDotListener.GetInstanceID()
    {
        return GetInstanceID();
    }
}
